home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / newmarch.zip / CATCH.C < prev    next >
C/C++ Source or Header  |  1992-09-08  |  8KB  |  265 lines

  1. /* Author:   $Author: jan $
  2.  * File:     $Source: /usr/usrs/jan/desktop/X_Book.boo/programs/RCS/catch.c,v $
  3.  * Date:     $Date: 1992/09/09 00:09:48 $
  4.  * Revision: $Revision: 1.1 $
  5.  */
  6.  
  7. #include "copyright.h"
  8.  
  9. /*     
  10. ** Chase a box around the screen, 
  11. ** attempting to click the    
  12. ** mouse in the box.    
  13. ** Scores are kept of your accuracy    
  14. */    
  15.     
  16. #include <stdio.h>    
  17. #include <X11/Xlib.h>    
  18. #include <X11/Xutil.h>    
  19.     
  20. #define DEBUG    
  21.     
  22. #define BOX_SIZE 4  /* in pixels */   
  23. #define TEXT_HT 20   /* in pixels - and a kludge */ 
  24. char WINDOW_NAME[] = "catch";    
  25. char ICON_NAME[] = "catch";    
  26.     
  27. Display *display;      /* the display device */    
  28. int     screen;        /* the screen on the display */
  29. int     theDepth;      /* number of color planes */  
  30. GC      gc;            /* graphics context */    
  31.     
  32. Window frame_window;   /* holds all the other windows */
  33. Window hit_window;     /* the count of hits */    
  34. Window miss_window;    /* the count of misses */    
  35. Window pane_window;    /* the background window for
  36.                          the box to move in */    
  37. Window box_window;     /* the box to hit with the
  38.                           cursor */    
  39. unsigned long  foreground, background;    
  40. int hits = 0;          /* start off with zero score */
  41. int misses = 0;        /* start off with zero score */  
  42.     
  43. int frame_x, frame_y, 
  44.     frame_width, frame_ht;  /* size of frame window */    
  45. /*    
  46. ** function openWindow    
  47. */    
  48.     
  49. Window    
  50. openWindow (x, y, width, height, border_width, parent,
  51.             istoplevel, argc, argv)    
  52.     int x, y;  /* coords of the upper left 
  53.                   corner in pixels */    
  54.     int width,    
  55.             height;  /* size of the window in pixels */    
  56.     int border_width;  /* the border width is
  57.                           not included in the    
  58.                           other dimensions */    
  59.     Window parent;   
  60.     int istoplevel;  /* a Boolean value */    
  61.     int argc;    
  62.     char **argv;    
  63. {    
  64.     Window  new_window;    
  65.     XSizeHints  size_hints;    
  66.         
  67.     /* now create the window */    
  68.     new_window = XCreateSimpleWindow (display,     
  69.                 parent,    
  70.                 x, y, width, height,    
  71.                 border_width,    
  72.                 foreground, background);    
  73.     
  74.     /* If the window is a toplevel window,    
  75.        set up the size hints for the window manager */    
  76.     if (istoplevel)    
  77.     {    
  78.           size_hints.x = x;    
  79.           size_hints.y = y;    
  80.           size_hints.width = width;    
  81.           size_hints.height = height;    
  82.           /* and state what hints are included */    
  83.           size_hints.flags = PPosition | PSize;    
  84.         
  85.           /* let the window manager know about
  86.              the window */    
  87.           XSetStandardProperties (display, new_window,
  88.                 WINDOW_NAME, ICON_NAME,    
  89.                 None,  /* no icon map */    
  90.                 argv, argc, &size_hints);    
  91.     }    
  92.     /* Display the window on the screen */    
  93.     XMapWindow (display, new_window);    
  94.     
  95.     /* Return the window ID */    
  96.     return (new_window);    
  97. }    
  98.     
  99. /*    
  100. ** function getGC    
  101. **    
  102. ** create a graphics context using default values, and
  103. ** return it in the pointer gc    
  104. */    
  105. GC
  106. getGC ()    
  107. {   GC gc;
  108.     
  109.     gc = XCreateGC (display, frame_window, 0, NULL);  
  110.  
  111.     XSetBackground (display, gc, background);    
  112.     XSetForeground (display, gc, foreground);    
  113.     return (gc);
  114. }    
  115.     
  116. /*    
  117. ** function quitX    
  118. **    
  119. ** terminate the program gracefully    
  120. */    
  121. quitX ()    
  122. {    
  123.     XCloseDisplay (display);    
  124.     exit (0);    
  125. }    
  126.     
  127. void    
  128. doExposeEvent (pEvent)    
  129.     XExposeEvent *pEvent;    
  130. {    
  131.     if  (pEvent->window == hit_window)    
  132.     {      char hit_str[20];    
  133.     
  134.           sprintf (hit_str, "Hits: %d", hits);    
  135.           XDrawImageString (display, hit_window, gc,    
  136.                 5, 10, hit_str, strlen (hit_str));    
  137.     }    
  138.     if (pEvent-> window == miss_window)    
  139.     {      char miss_str[20];    
  140.     
  141.           sprintf (miss_str, "Misses: %d", misses);    
  142.           XDrawImageString (display, miss_window, gc,
  143.                 5, 10, miss_str, strlen (miss_str));    
  144.     }    
  145. }    
  146.     
  147. void
  148. doButtonPressEvent (pEvent)    
  149.     XButtonEvent *pEvent;    
  150. {   int box_x, box_y;    
  151.     
  152.     if (pEvent->window == pane_window)    
  153.     {    
  154.           misses++;    
  155.           XClearArea (display, miss_window, 0, 0,     
  156.                 0, 0, True);    
  157.     }    
  158.     else /* pEvent->window == box_window */    
  159.     {    
  160.           hits++;    
  161.           XClearArea (display, hit_window, 0, 0,    
  162.                 0, 0, True);    
  163.     }    
  164.     box_x = rand () % (frame_width - BOX_SIZE);    
  165.     box_y = rand () % (frame_ht - TEXT_HT - BOX_SIZE); 
  166.     XMoveWindow (display, box_window, box_x, box_y);    
  167. }    
  168.     
  169. void    
  170. doKeyPressEvent (pEvent)    
  171.     XKeyEvent *pEvent;    
  172. {   int key_buffer_size = 10;    
  173.     char key_buffer[64];    
  174.     XComposeStatus compose_status;    
  175.     KeySym key_sym;    
  176.     
  177.     XLookupString (pEvent, key_buffer, key_buffer_size, 
  178.           &key_sym, &compose_status);    
  179.     if (key_buffer[0] == 'q')    
  180.     {      printf ("Hits: %d; Misses: %d\n", 
  181.                    hits, misses);    
  182.           quitX ();    
  183.     }    
  184. }    
  185.     
  186. initX () 
  187. {    
  188.     display = XOpenDisplay (NULL);    
  189.     screen = DefaultScreen (display);    
  190.     /* use the default foreground and 
  191.        background colors */
  192.     foreground = BlackPixel (display, screen);    
  193.     background = WhitePixel (display, screen);    
  194. }    
  195. main (argc, argv)    
  196.     int argc;    
  197.     char **argv;    
  198. {   XEvent event;    
  199.     int box_x, box_y;    
  200.     
  201.     initX ();
  202.     
  203.     /* create the frame window to occupy a 
  204.        large area of the screen,    
  205.        in the middle */    
  206.     frame_width = DisplayWidth (display, screen) / 2; 
  207.     frame_ht = (DisplayHeight (display, screen)*2) / 3;
  208.     frame_x = frame_width / 2;    
  209.     frame_y = frame_ht / 6;    
  210.     frame_window = openWindow (frame_x, frame_y,
  211.                       frame_width, frame_ht, 5,
  212.                       DefaultRootWindow (display),    
  213.                       True, argc, argv);    
  214.     hit_window = openWindow (0, 0, frame_width / 2,
  215.                       TEXT_HT,
  216.                       1, frame_window, False, 0, NULL);
  217.     miss_window = openWindow (frame_width / 2, 0,     
  218.                       frame_width / 2, TEXT_HT,    
  219.                       1, frame_window, False, 0, NULL);
  220.     pane_window = openWindow (0, TEXT_HT,    
  221.                       frame_width, frame_ht - TEXT_HT, 
  222.                       1, frame_window, False, 0, NULL);
  223.     srand (1);    
  224.     box_x = rand () % (frame_width - BOX_SIZE);    
  225.     box_y = rand () % (frame_ht - TEXT_HT - BOX_SIZE);
  226.     box_window = openWindow (box_x, box_y, BOX_SIZE,
  227.                       BOX_SIZE, 1,
  228.                       pane_window, False, 0, 
  229.                       NULL);
  230.     gc = getGC ();     
  231.     XSelectInput (display, frame_window,    
  232.           KeyPressMask);    
  233.     XSelectInput (display, pane_window,    
  234.           ButtonPressMask);    
  235.     XSelectInput (display, hit_window,    
  236.           ExposureMask);    
  237.     XSelectInput (display, miss_window,    
  238.                        ExposureMask);    
  239.     XSelectInput (display, box_window,    
  240.                        ButtonPressMask);    
  241.         
  242.     while (True)    
  243.     {    
  244.           XNextEvent (display, &event);    
  245. #ifdef DEBUG    
  246.           fprintf (stderr, "Event number is %d\n",
  247.                    event.type);    
  248. #endif    
  249.           switch (event.type)    
  250.           {    
  251.           case Expose:doExposeEvent (&event);    
  252.                       break;   
  253.           case ButtonPress:    
  254.                       doButtonPressEvent (&event);    
  255.                       break;   
  256.           case KeyPress:
  257.                       doKeyPressEvent (&event);    
  258.                       break;    
  259.           case MappingNotify:    
  260.                        XRefreshKeyboardMapping (&event);
  261.                       break;    
  262.           }    
  263.     }    
  264.